package com.secondhand.project.service.impl;

import cn.hutool.core.date.DateUtil;
import com.secondhand.common.utils.SecurityUtils;
import com.secondhand.project.domain.Orders;
import com.secondhand.project.domain.OrdersStatus;
import com.secondhand.project.mapper.OrdersMapper;
import com.secondhand.project.service.IOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrdersServiceImpl implements IOrdersService
{
    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    public Orders selectOrdersById(Long id)
    {
        return ordersMapper.selectOrdersById(id);
    }

    @Override
    public List<Orders> selectOrdersList(Orders orders)
    {
        return ordersMapper.selectOrdersList(orders);
    }

    @Override
    public int insertOrders(Orders orders) {
        orders.setSaleId(orders.getUserId());
        orders.setUserId(SecurityUtils.getUserId());
        orders.setStatus(OrdersStatus.UNPAID);
        orders.setTime(DateUtil.now());
        orders.setOrderNo(DateUtil.format(DateUtil.date(), "yyyyMMddHHmmss"));
        return ordersMapper.insertOrders(orders);
    }

    @Override
    public int updateOrders(Orders orders) {
        if (orders.getStatus().equals(OrdersStatus.PAID)) {
            orders.setPayNo("pay_" + DateUtil.format(DateUtil.date(), "yyyyMMddHHmmss"));
            orders.setPayTime(DateUtil.now());
        }
        return ordersMapper.updateOrders(orders);
    }

    @Override
    public int deleteOrdersByIds(Long[] ids)
    {
        return ordersMapper.deleteOrdersByIds(ids);
    }

    @Override
    public int deleteOrdersById(Long id)
    {
        return ordersMapper.deleteOrdersById(id);
    }
}
